import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class HttpServer {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;

        if (args.length == 0) {
            try {
                serverSocket = new ServerSocket(80);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                serverSocket = new ServerSocket(Integer.parseInt(args[0]));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            Socket server = serverSocket.accept();
            System.out.println("连接到服务器: " + server);
            service(server);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void service(Socket socket) {
        try {
            InputStream inputStream = socket.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);


            OutputStream outputStream = socket.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);

            String line = null;
            line = bufferedReader.readLine();
            System.out.println("请求行");
            System.out.println(line);
            String[] requestLine = line.split(" ");
            // for (String s : requestLine) {
            //     System.out.println(s);
            // }

            System.out.println("请求头");
            while (!(line = bufferedReader.readLine()).isEmpty()) {
                System.out.println(line);
            }
            System.out.println(line);

            // while ((line = bufferedReader.readLine()) != null) {
            //     System.out.println(line);
            // }


            System.out.println("请求资源");
            System.out.println(requestLine[1]);
            if (requestLine[1].equals("/")){
                bufferedWriter.write("HTTP/1.1 200 OK");
                bufferedWriter.write("\r\n");


                bufferedWriter.write("Date: Fri, 26 May 2023 01:24:10 GMT");
                bufferedWriter.write("\r\n");
                bufferedWriter.write("Content-Type: text/html;charset=utf-8");
                bufferedWriter.write("\r\n");
                bufferedWriter.write("\r\n");
                // bufferedWriter.write("Hello, World!");
                FileInputStream fileInputStream = new FileInputStream("index.html");
                InputStreamReader inputStreamReader1 = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader1 = new BufferedReader(inputStreamReader1);
                String temp;
                while ((temp = bufferedReader1.readLine()) != null) {
                    bufferedWriter.write(temp);
                }
                bufferedReader1.close();
                inputStreamReader1.close();
                fileInputStream.close();
                bufferedWriter.flush();


            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
